using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
public class UtilFile
{
	public static void delete_dir(string dir_path)
	{
		string[] files = Directory.GetFiles(dir_path);
		for (int i = 0; i < files.Length; i++)
		{
			string path = files[i];
			File.Delete(path);
		}
	}
	public static void copy_dir(string ori_dir_path, string to_dir_path)
	{
		if (!Directory.Exists(to_dir_path))
		{
			Directory.CreateDirectory(to_dir_path);
		}
		string[] directories = Directory.GetDirectories(ori_dir_path);
		for (int i = 0; i < directories.Length; i++)
		{
			string text = directories[i];
			UtilFile.copy_dir(text + "\\", to_dir_path + Path.GetFileName(text) + "\\");
		}
		string[] files = Directory.GetFiles(ori_dir_path);
		for (int j = 0; j < files.Length; j++)
		{
			string text2 = files[j];
			File.Copy(text2, to_dir_path + Path.GetFileName(text2), true);
		}
	}
	public static string read_str(string string_0)
	{
		return UtilFile.read(string_0, Encoding.GetEncoding("GB2312"));
	}
	public static string get_file_md5(string file_path)
	{
		string result;
		try
		{
			if (File.Exists(file_path + "e"))
			{
				File.Delete(file_path + "e");
			}
			File.Copy(file_path, file_path + "e");
			FileStream fileStream = new FileStream(file_path + "e", FileMode.Open);
			byte[] array = new MD5CryptoServiceProvider().ComputeHash(fileStream);
			fileStream.Close();
			fileStream.Dispose();
			StringBuilder stringBuilder = new StringBuilder();
			for (int i = 0; i < array.Length; i++)
			{
				stringBuilder.Append(array[i].ToString("x2"));
			}
			result = stringBuilder.ToString();
		}
		catch (Exception ex)
		{
			throw new Exception("GetMD5HashFromFile() fail,error:" + ex.Message);
		}
		finally
		{
			try
			{
				File.Delete(file_path + "e");
			}
			catch
			{
			}
		}
		return result;
	}
	public static bool write(string file_path, byte[] byte_0)
	{
		bool result;
		try
		{
			if (File.Exists(file_path))
			{
				File.Delete(file_path);
			}
			FileStream fileStream = new FileStream(file_path, FileMode.Create);
			fileStream.Write(byte_0, 0, byte_0.Length);
			fileStream.Close();
			fileStream.Dispose();
			result = true;
		}
		catch (Exception)
		{
			result = false;
		}
		return result;
	}
	public static byte[] read(string file_path)
	{
		FileInfo fileInfo = new FileInfo(file_path);
		long length = fileInfo.Length;
		FileStream fileStream = new FileStream(file_path, FileMode.Open);
		byte[] array = new byte[length];
		fileStream.Read(array, 0, (int)length);
		fileStream.Close();
		fileStream.Dispose();
		return array;
	}
	public static bool write(string file_path, string content, Encoding encoding)
	{
		bool result;
		try
		{
			if (File.Exists(file_path))
			{
				File.Delete(file_path);
			}
			FileStream fileStream = new FileStream(file_path, FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream, encoding);
			streamWriter.Write(content);
			streamWriter.Flush();
			streamWriter.Close();
			streamWriter.Dispose();
			fileStream.Close();
			fileStream.Dispose();
			result = true;
		}
		catch (Exception)
		{
			result = false;
		}
		return result;
	}
	public static bool write(string file_path, string content)
	{
		return UtilFile.write(file_path, content, Encoding.GetEncoding("GB2312"));
	}
	public static bool write_add(string file_path, string content)
	{
		return UtilFile.write_add(file_path, content, Encoding.GetEncoding("GB2312"));
	}
	public static bool write_add(string file_path, string string_1, Encoding encoding_0)
	{
		bool result;
		try
		{
			FileStream fileStream = new FileStream(file_path, FileMode.Append);
			StreamWriter streamWriter = new StreamWriter(fileStream, encoding_0);
			streamWriter.Write(string_1);
			streamWriter.Flush();
			streamWriter.Close();
			streamWriter.Dispose();
			fileStream.Close();
			fileStream.Dispose();
			result = true;
		}
		catch (Exception)
		{
			result = false;
		}
		return result;
	}
	public static string read(string file_path, Encoding encoding)
	{
		string result;
		try
		{
			if (!File.Exists(file_path))
			{
				result = "";
			}
			else
			{
				StreamReader streamReader = new StreamReader(file_path, encoding);
				string text = streamReader.ReadToEnd();
				streamReader.Close();
				streamReader.Dispose();
				result = text;
			}
		}
		catch (Exception)
		{
			result = "";
		}
		return result;
	}
}
